#include "window2.h"
#include <QMessageBox>
#include <QGraphicsBlurEffect>
#include <QGraphicsColorizeEffect>
#include <QGraphicsDropShadowEffect>
#include <QGraphicsOpacityEffect>
#include <QCoreApplication>
#include <QDebug>
Window2::Window2(QWidget *parent) : QMainWindow(parent)
{
    setGeometry(300, 100, 800, 600);
    //场景绘制
    //drawScene();

    //移动图片
    movePic();
}
//场景绘制
void Window2::drawScene(){
    scene = new QGraphicsScene();
    scene->setForegroundBrush(QColor(0, 255,255,127));
    //字体设置
    QFont font("宋体",20);
    scene->addSimpleText("图形图像", font);

    //QPen 属性
    QPen pen;
    pen.setStyle(Qt::DashDotLine);
    pen.setWidth(2);
    pen.setBrush(Qt::black);
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    //绘制线条
    scene->addLine(-200, -100,200,100,pen);
    //绘制椭圆
    scene->addEllipse(50,50,100,120);

    //网格
    scene->setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern));//实例 QGraphicsView
    view = new QGraphicsView(scene);
    //显示
    this->setCentralWidget(view);
}
//移动图片
void Window2::movePic(){
    //实例 QLabel
    label = new QLabel(this);
    label->setGeometry(QRect(50,50,200,200));
    //实例 QImage
    img = new QImage;
    //QImage 加载图片
    bool  b = img->load(QCoreApplication::applicationDirPath().append("/resources/123.png"));
    if (!b){
        qDebug() << "图片加载失败";
    }
    //label 显示图片
    label->setPixmap(QPixmap::fromImage(*img));
    //实例 button
    int x = 100;
    int y = 300;
    moveBtn = new QPushButton(this);
    moveBtn->setGeometry(QRect(x-100,300,80,25));
    moveBtn->setText("移动");
    connect(moveBtn,SIGNAL(clicked()),this,SLOT(slotMoveImg()));


    //实例 button
    hReverseBtn = new QPushButton(this);
    hReverseBtn->setGeometry(QRect(x, y,80,25));
    hReverseBtn->setText("水平翻转");
    connect(hReverseBtn,SIGNAL(clicked()),this,SLOT(slotHReverseImg()));

    //实例 button
    vReverseBtn = new QPushButton(this);
    vReverseBtn->setGeometry(QRect(x+100, y,80,25));
    vReverseBtn->setText("垂直翻转");
    connect(vReverseBtn,SIGNAL(clicked()),this,SLOT(slotVReverseImg()));

    //实例 button
    rotateBtn = new QPushButton(this);
    rotateBtn->setGeometry(QRect(x+200, y,80,25));
    rotateBtn->setText("旋转");
    connect(rotateBtn,SIGNAL(clicked()),this,SLOT(slotRotateImg()));
    //实例 button
    biggerBtn = new QPushButton(this);
    biggerBtn->setGeometry(QRect(x -100 ,y+50,80,25));
    biggerBtn->setText("放大");
    connect(biggerBtn,SIGNAL(clicked()),this,SLOT(slotBiggerImg()));

    //实例 button
    smallerBtn = new QPushButton(this);
    smallerBtn->setGeometry(QRect(x, y+50,80,25));
    smallerBtn->setText("缩小");
    connect(smallerBtn,SIGNAL(clicked()),this,SLOT(slotSmallerImg()));

    //实例 button
    createImgBtn = new QPushButton(this);
    createImgBtn->setGeometry(QRect(x + 100, y+50,150,25));
    createImgBtn->setText("生成带文字的图片");
    connect(createImgBtn,SIGNAL(clicked()),this,SLOT(slotCreateImg()));

    //模糊
    blurImgBtn = new QPushButton(this);
    blurImgBtn->setGeometry(QRect(x + 300, y+50,80,25));
    blurImgBtn->setText("模糊");
    connect(blurImgBtn,SIGNAL(clicked()),this,SLOT(slotBlurImg()));

    //着色
    colorImgBtn = new QPushButton(this);
    colorImgBtn->setGeometry(QRect(x + 400, y+50,80,25));
    colorImgBtn->setText("着色");
    connect(colorImgBtn,SIGNAL(clicked()),this,SLOT(slotColorImg()));

    //阴影
    shadowImgBtn = new QPushButton(this);
    shadowImgBtn->setGeometry(QRect(x + 500, y+50,80,25));
    shadowImgBtn->setText("阴影");
    connect(shadowImgBtn,SIGNAL(clicked()),this,SLOT(slotShadowImg()));

    //透明
    opacityImgBtn = new QPushButton(this);
    opacityImgBtn->setGeometry(QRect(x + 600, y+50,80,25));
    opacityImgBtn->setText("透明");
    connect(opacityImgBtn,SIGNAL(clicked()),this,SLOT(slotOpacityImg()));
}
//移动图片
//点击移动
void Window2::slotMoveImg(){
    //移动 label 控件
    label->move(i,50);
    i += 10;

}

//水平翻转
void Window2::slotHReverseImg(){
    *img = img->mirrored(true, false);
    label->setPixmap(QPixmap::fromImage(*img));
}
//垂直翻转
void Window2::slotVReverseImg(){
    *img = img->mirrored(false, true);
    label->setPixmap(QPixmap::fromImage(*img));
}
//旋转
void Window2::slotRotateImg(){
    QMatrix matrix;
    //88 度角
    matrix.rotate(90);
    *img = img->transformed(matrix);
    label->setPixmap(QPixmap::fromImage(*img));
}
//放大QGraphicsBlurEffect
void Window2::slotBiggerImg(){
    *img = img->scaled(200 , 200, Qt::IgnoreAspectRatio);
    label->setPixmap(QPixmap::fromImage(*img));
}
//缩小
void Window2::slotSmallerImg(){
    *img = img->scaled(100, 100, Qt::IgnoreAspectRatio);
    label->setPixmap(QPixmap::fromImage(*img));
}
//生成带文字的图片
void Window2::slotCreateImg(){
    //实例 QPainter
    QPainter painter(img);
    //设置画刷模式
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    //改变画笔和字体
    QPen pen = painter.pen();
    pen.setColor(Qt::yellow);
    QFont font = painter.font();
    font.setBold(true);//加粗
    font.setPixelSize(30);//改变字体大小
    painter.setPen(pen);
    painter.setFont(font);
    //将文字绘制在图片中心位置
    painter.drawText(img->rect(),Qt::AlignCenter,"你好,朋友。");
    //这个为图片的压缩度。0/100
    int n = 100;
    //保存图片
    img->save("text.png","PNG",n);

    QMessageBox::about(this, "提示","创建成功");

}
//重写
void Window2::paintEvent(QPaintEvent *){
    //实例 QPainter
    QPainter painter(this);
    //实例 QPixmap
    QPixmap pix;
    //加载图片
    pix.load("resources/123.jpg");
    //原图显示
    painter.drawPixmap(300,50,183,160,pix);
    //扭曲
    painter.shear(0.5,0); //横向扭曲
    //绘制扭曲图
    painter.drawPixmap(500,50,183,160,pix);

}

//模糊效果
void Window2::slotBlurImg(){
    //模糊
    QGraphicsBlurEffect *effect = new QGraphicsBlurEffect(this);
    //模糊值,值越大越模糊
    effect->setBlurRadius(5);

    label->setGraphicsEffect(effect);
}
//着色
void Window2::slotColorImg(){
    //着色
    QGraphicsColorizeEffect  *effect = new QGraphicsColorizeEffect (this);
    effect->setColor(QColor(0, 0, 192));
    label->setGraphicsEffect(effect);

}

//阴影
void Window2::slotShadowImg(){
    //实例 QGraphicsDropShadowEffect
    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
    //设定阴影
    effect->setOffset(8,8);
    label->setGraphicsEffect(effect);

}

//透明效果
void Window2::slotOpacityImg(){
    //实例 QGraphicsOpacityEffect
    QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
    //设定透明值
    effect->setOpacity(0.5);
    label->setGraphicsEffect(effect);

}





